var scope = ['$scope','$http', function($scope, $http) {

    $http.defaults.useXDomain = true;

    $scope.users = [];

    $http.get('http://127.0.0.1:9000/exingcai/cualuser').success(function (data, status, headers, config) {
        $scope.users = data;
    }).error(function (data, status, headers, config) {
        $scope.errorMessage = "Can't retrieve cualuser list!";
    });
    
    $scope.getCualUser = function(id) {
         $http.get('http://127.0.0.1:9000/exingcai/cualuser/'+id).success(function (data, status, headers, config) {
                $scope.users = data;
            }).error(function (data, status, headers, config) {
                $scope.errorMessage = "Can't retrieve cualuser!";
            });
    };
    
    $scope.delCualUser = function(id) {
     $http.put('http://127.0.0.1:9000/exingcai/cualuser/'+id).success(function (data, status, headers, config) {
            $scope.users = data;
        }).error(function (data, status, headers, config) {
            $scope.errorMessage = "Can't del cualuser!";
        });
   };

    $scope.putCualUser = function() {
        var user = $scope.user;
        var params = JSON.stringify(user);
        $http.put('http://127.0.0.1:9000/exingcai/cualuser/put', params, {
            headers: {
                'Content-Type': 'application/json; charset=UTF-8'
            }
        }).success(function (data, status, headers, config) {
            if(data.id) {
                $scope.users.push(data);
                // $scope.users = {};
                $scope.errorMessage = "cualuser id: " + data.id + " is created.";
            } else {
                $scope.errorMessage = "put cualuser failed!"
            }
        }).error(function (data, status, headers, config) {
            $scope.errorMessage = "put cualuser failed!"
        });
    };
}];

return scope;

